<template>
  <!-- 课程列表 -->
  <div>
    <div class="list-wrapper flex-wrap " v-if="!vertical">
      <div class="list-item" :style="isMark ? markPageStyle : null" v-for="(item, index) in list" :key="index" @click="toPage(item)">
        <div class="list-item-img">
          <div class="item-default-img flex-wrap flex-middle flex-center" :style="{
                        background: 'url(' + item.courseDto.imageHttpUrl + ') center no-repeat'
                    }" v-if="item.courseDto && item.courseDto.imageHttpUrl">
            <div  class="abs-tip-price font-14 flex-wrap flex-center flex-text-end" v-if="item.courseDto.needPayFlag === '1'">
              <p class="fs-14">￥{{item.courseDto.viewPrice || 0 }}</p>
            </div>
            <div class="item-default-text ellipsis2" v-if="showTitle === '1'">{{item.courseDto && item.courseDto.name || item.title}}</div>
          </div>
          <div class="item-default-img flex-wrap flex-middle flex-center" v-else>
            <div  class="abs-tip-price font-14 flex-wrap flex-center flex-text-end" v-if="item.courseDto.needPayFlag === '1'">
              <p class="fs-14">￥{{item.courseDto.viewPrice || 0 }}</p>
            </div>
            <div class="item-default-text ellipsis2">{{item.courseDto && item.courseDto.name || item.title}}</div>
          </div>
          <div class="img-icon-wrap flex-wrap">
            <span class="img-icon yellow" v-if="item.courseDto && item.courseDto.isTop == 1">置顶</span>
            <span class="img-icon green" v-if="item.courseDto && item.courseDto.isRecommend == 1 && !hideReco">推荐</span>
            <span class="img-icon red" v-if="item.courseDto && item.courseDto.liveStatus == 1">进行中</span>
          </div>
        </div>
        <div class="item-title fs-14 fc-333 ellipsis bold">{{item.courseDto && item.courseDto.name || item.title}}</div>
        <div class="item-data flex-wrap flex-middle">
          <template v-if="item.courseDto.type == 3">
            <span class="fs-12 fc-999 flex-item">{{ item.courseDto.startShowDate }}</span>
            <span class="fs-12 fc-999">{{ item.courseDto.mainTeacherName }}</span>
          </template>
          <template v-else>
            <span class="fs-12 fc-999 flex-item letter">{{item.courseDto && item.courseDto.studyStuNum || 0}}人{{ item.courseDto.type == 1 ? '加入学习' : '报名' }}</span>
            <template v-if="item.courseDto.type == 1">
                          <span class="fs-12 fc-999">
                            <template v-if="!isPubc(item)">
                              <span :class="[item.regType == 1 ? 'primary-color' : 'yellow-color']">{{ dealRegType(item.regType) }}</span>
                              <span v-if="dealRegType(item.regType)" class="tips-split">|</span>
                            </template>共{{ item.courseDto && item.courseDto.lessonNum || 0 }}课时</span>
            </template>
            <template v-else>
              <span class="fs-12 fc-999 ellipsis loc"><van-icon name="location-o" />{{ item.applyAddress }}</span>
            </template>
          </template>
        </div>
        <div class="mark-btn" v-if="isMark" @click.stop="cancelMark(item)">取消收藏</div>

      </div>
    </div>
    <div class="list-wrapper flex-wrap vertical" v-else>
      <div class="list-item flex-wrap" v-for="(item, index) in list" :key="index" @click="toPage(item)">
        <template v-if="item.courseDto">
          <div class="list-item-img">
            <div class="item-default-img flex-wrap flex-middle flex-center" :style="{
                            background: 'url(' + item.courseDto.imageHttpUrl + ') center no-repeat'
                        }" v-if="item.courseDto && item.courseDto.imageHttpUrl">
              <div class="item-default-text ellipsis2" v-if="showTitle === '1'">{{item.courseDto && item.courseDto.name || item.title}}</div>
            </div>
            <div class="item-default-img flex-wrap flex-middle flex-center" v-else>
              <div class="item-default-text ellipsis2">{{item.courseDto && item.courseDto.name || item.title}}</div>
            </div>
            <div class="img-icon-wrap flex-wrap">
              <span class="img-icon yellow" v-if="item.courseDto && item.courseDto.isTop == 1">置顶</span>
              <span class="img-icon green" v-if="item.courseDto && item.courseDto.isRecommend == 1 && !hideReco">推荐</span>
              <span class="img-icon red" v-if="item.courseDto && item.courseDto.liveStatus == 1">进行中</span>
            </div>

            <!-- price-->

            <!-- progress -->
            <div v-if="progress && item.courseDto.type == 1" class="progress-bar">
              <div class="black-bar"></div>
              <div class="green-bar" v-if="item.studyStatus != 1" :style="{ width: `${calPencentage(item)}%` }"></div>
              <div class="progress-number">{{ item.studyStatus != 1 ? `已学 ${calPencentage(item)}%` : '未开始' }}</div>
            </div>
            <div class="pub-corner" v-if="isMine && isPubc(item)">
              <span>{{item.courseDto && item.courseDto.publicCourseLabel || item.publicCourseLabel || '公开课'}}</span>
            </div>
          </div>

          <div class="list-right flex-item flex-wrap flex-column space-between">

            <div class="item-title fs-14 fc-333 ellipsis ">
              <!--  -->
              <!-- <img  :src="newTipImg" alt=""> -->
              <i v-if="item.courseDto && item.hadNewChapter == 1" class="icon_new">新课时</i>
              <span v-if="item.courseDto && item.courseDto.isNewEmployeeTask == 1"  class="bold abs-tip-staff-yellow" style="margin-left: 4px;" >新员工</span>
              {{item.courseDto && item.courseDto.name || item.title}}
              <div v-if="item.courseDto.needPayFlag === '1'">
                <p class="fc-buy fs-14 bold">￥{{item.courseDto.viewPrice || 0 }}</p>
              </div>
              <div class="mt5">
                <span class="fs-12 fc-999" v-if="(item.courseDto&& item.courseDto.categoryName) ||item.categoryName"> 分类：{{ (item.courseDto&& item.courseDto.categoryName) ||item.categoryName }}</span>
              </div>
            </div>

            <div class="item-icon-wrap flex-wrap flex-middle" v-if="isMine">
              <span class="item-icon" v-if="item.courseDto && dealCourseType(item.courseDto.type) != '在线'">{{ dealCourseType(item.courseDto.type) }}</span>
            </div>

            <div class="item-data flex-wrap flex-middle">
              <template v-if="item.courseDto.type == 3">
                <span class="fs-12 fc-999 flex-item">{{ item.courseDto.startShowDate }}</span>
                <span class="fs-12 fc-999">{{ item.courseDto.mainTeacherName }}</span>
              </template>
              <template v-else>
                <span class="fs-12 fc-999 flex-item">{{item.courseDto && item.courseDto.studyStuNum || 0}}人已{{ item.courseDto.type == 1 ? '加入学习' : '报名' }}</span>
                <template v-if="item.courseDto.type == 1">
                  <span class="fs-12 fc-999"><template v-if="!isPubc(item)"><span :class="[item.regType == 1 ? 'primary-color' : 'yellow-color']">{{ dealRegType(item.regType) }}</span><span v-if="dealRegType(item.regType)" class="tips-split">|</span></template>共{{item.courseDto && item.courseDto.lessonNum || 0}}课时</span>
                </template>
                <template v-else>
                  <span class="fs-12 fc-999 ellipsis loc"><van-icon name="location-o" />{{ item.applyAddress }}</span>
                </template>
              </template>
            </div>
            <div class="mark-btn" v-if="isMark" @click="cancelMark(item)">取消收藏</div>
          </div>
        </template>
        <template v-else-if="isTeacher">
          <div class="list-item-img">
            <div class="item-default-img flex-wrap flex-middle flex-center" :style="{
                            background: 'url(' + item.imageHttpUrl + ') center no-repeat'
                        }" v-if="item.imageHttpUrl">
              <div class="item-default-text ellipsis2" v-if="showTitle === '1'">{{item.name}}</div>
            </div>
            <div class="item-default-img flex-wrap flex-middle flex-center" v-else>
              <div class="item-default-text ellipsis2">{{item.name}}</div>
            </div>
            <div class="img-icon-wrap flex-wrap">
              <span class="img-icon yellow" v-if="item.isTop == 1">置顶</span>
              <span class="img-icon green" v-if="item.isRecommend == 1 && !hideReco">推荐</span>
              <span class="img-icon red" v-if="item.liveStatus == 1">进行中</span>
            </div>
          </div>
          <div class="list-right flex-item flex-wrap flex-column space-between">
            <div class="item-title fs-14 fc-333 ellipsis bold">
              <!--  -->
              <!-- <img  :src="newTipImg" alt=""> -->
              <i v-if="item.hadNewChapter == 1" class="icon_new">新课时</i>
              <span v-if="item.isNewEmployeeTask == 1"  class="abs-tip-staff-yellow" style="margin-left: 4px;" >新员工</span>
              {{item.name}}
              <div v-if="item.needPayFlag === '1'">
                <p class="fc-buy fs-14 bold">￥{{item.viewPrice || 0 }}</p>
              </div>
            </div>
            <span class="fs-12 fc-999" v-if="item.categoryName"> 分类：{{ item.categoryName }}</span>


            <div class="item-data flex-wrap flex-middle">
              <template v-if="item.type == 3">
                <span class="fs-12 fc-999 flex-item">{{ item.startShowDate }}</span>
                <span class="fs-12 fc-999">{{ item.mainTeacherName }}</span>
              </template>
              <template v-else>
                <span class="fs-12 fc-999 flex-item">{{item.studyStuNum || 0}}人已{{ item.type == 1 ? '加入学习' : '报名' }}</span>
                <template v-if="item.type == 1">
                  <span class="fs-12 fc-999"><template v-if="!isPubc(item)"><span :class="[item.regType == 1 ? 'primary-color' : 'yellow-color']">{{ dealRegType(item.regType) }}</span><span v-if="dealRegType(item.regType)" class="tips-split">|</span></template>共{{item.lessonNum || 0}}课时</span>
                </template>
                <template v-else>
                  <span class="fs-12 fc-999 ellipsis loc"><van-icon name="location-o" />{{ item.applyAddress }}</span>
                </template>
              </template>
            </div>
            <div class="mark-btn" v-if="isMark" @click="cancelMark(item)">取消收藏</div>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { dealRegType } from '@/assets/js/util'
import {allJobStage, courseTypes} from '@/assets/js/constant'
// import newTipImg from '@/static/image/new_course_tip.png'
export default {
  data() {
    return {
      // newTipImg: newTipImg
      markPageStyle: {
        'padding': '6px 8px 10px',
        'border-radius': '4px',
        'background-color': '#fff',
        'margin-bottom': '10px',
      }
    }
  },
  props: {
    list: {
      type: Array,
      default: () => []
    },
    vertical: { // 竖版显示
      type: Boolean,
      default: false
    },
    isTeacher: { // 竖版显示
      type: Boolean,
      default: false
    },
    pageKey: { // 跳转id的key
      type: String,
      default: ''
    },
    progress: { // 是否显示课程学习进度（我的课程有效）, 但是面授和会议不展示
      type: Boolean,
      default: false
    },
    hideReco: {
      type: Boolean,
      default: false
    },
    isMine: { // 是否是我的课程或者培训课程
      type: Boolean,
      default: false
    },
    isMark: { // 是否为收藏
      type: Boolean,
      default: false
    },
    siteType: {
      type: String,
      default: ''
    }
  },
  computed: {
    ...mapGetters({
      showTitle: 'siteInfo/showTitle'
    })
  },
  mounted() {
  },
  methods: {
    isPubc(item) {
      return item.courseDto ? (item.courseDto.courseType === 'PUBC' || item.courseDto.isPublic == 1) : false
    },
    dealRegType(type) {
      return dealRegType(type)
    },
    dealCourseType(type) {
      let current = courseTypes.find(e => e.value == type)
      return current ? current.name : ''
    },
    genTitleName(type, name="" ) {
      if (type === "PUBC" || type === "C") {
        return "【课程】" + name
      } else if (type === "T") {
        return "【培训项目】" + name
      } else if (type === "PC") {
        return "【认证】" + name
      }else if (type === "L") {
        return "【直播】" + name
      }else {
        return `【${this.getType(type).name}】` + name
      }
    },
    getType(item) {
      return item && allJobStage.find((ms) => ms.type === item) || {}
    },
    toPage(item) {
      let query = Object.assign({}, {siteType: this.siteType})
      this.$uniPush({
        name: 'course-detail-id',
        params: {
          id: item.courseId
        },
        query:query
      })
    },

    calPencentage(item) {
      return item.studyStatus != 1 ? (item.courseDto.lessonNum ? Number((item.courseDto.compleltedLessonNum / item.courseDto.lessonNum * 100).toFixed(2)) : 0) : 0
    },
    cancelMark(row){
      this.$api.course.delCourseMark({ courseId: row.courseId })
          .then((res) => {
            this.$toast.success(`取消收藏成功`)
            this.$emit("reload-list")
          })
          .catch((e) => {
            this.$toast.fail(`取消收藏失败` + (e.msg ? "：" + e.msg : ""))
          })
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/static/css/variable.scss';
.list-wrapper{
  flex-wrap: wrap;
  padding-bottom: 4px;
  .icon_new{
    display: inline-block;
    padding: 0 4px;
    height: 16px;
    line-height: 16px;
    font-size: 11px;
    font-weight: 400;
    color: #FFFFFF;
    border-radius: 24px 24px 24px 2px;
    vertical-align: middle;
    background: linear-gradient(120deg, #fe8d30 0%, #fe1c55 100%);
  }
  &.vertical{
    .list-item{
      width: 100%;
      padding: 15px 0;
      &:nth-child(2n){
        margin-left: 0;
      }
      .list-item-img{
        width: 38.4vw;
        height: 21.6vw;
        margin-bottom: 0;
        position: relative;
        .pub-corner {
          top: 0;
          right: 0;
        }
        .item-default-img{
          min-width: 38.4vw;
          width: 38.4vw;
          height: 21.6vw;
          position: relative;

        }
        .progress-bar {
          height: 20px;
          border-radius: 0px 0px 2px 2px;
          position: absolute;
          bottom: 0;
          width: 100%;
          .black-bar {
            background: #000000;
            opacity: 0.39;
            height: 100%;
          }
          .green-bar {
            background: $primaryColor;
            opacity: 0.74;
            height: 100%;
            bottom: 0;
            position: absolute;
          }
          .progress-number {
            color: #fff;
            font-size: 11px;
            bottom: 2px;
            width: 100%;
            position: absolute;
            text-align: center;
          }
        }
      }
      .item-icon-wrap{
        height: 24px;
        //margin-bottom: 16px;
      }
      .list-right{
        width: calc(100% - 38.4vw - 9px);
        margin-left: 9px;
        .item-title {
          img {
            width: 41px;
            margin-right: 3px;
            vertical-align: -2px;
          }
        }
      }
    }
  }
  .list-item{
    width: calc((100% - 13px)/2);
    padding: 6px 0 10px;
    position: relative;
    &:nth-child(2n){
      margin-left: 13px;
    }

    .list-item-img{
      position: relative;
      width: 100%;
      height: 24.8vw;
      margin-bottom: 5px;
      .item-default-img{
        width: 100%;
        height: 24.8vw;
        background: url("@/static/image/course-img.png") no-repeat;
        background-size: contain !important;
        .abs-tip-price {
          position: absolute;
          left: 0;
          bottom: 0;
          height: 22px;
          background: #F9563D;
          color: #fff;
          border-radius: 0 8px 0 0;
          padding: 0 5px;
          line-height: 25px;
          flex-wrap: nowrap;
          .icon {
            transform:scale(0.8);
          }
        }
        .item-default-text{
          font-size: 12px;
          color: #fff;
          text-align: center;
          padding: 0 10px;
        }
      }
      .img-icon-wrap{
        position: absolute;
        left: 4px;
        top: 4px;
        .img-icon{
          padding: 0 4px;
          border-radius: 2px;
          font-size: 10px;
          height: 16px;
          color: #fff;
          line-height: 16px;
          & + .img-icon{
            margin-left: 4px;
          }
          &.yellow {
            background: linear-gradient(342deg, #F57923 0%, #F5A623 100%);
          }
          &.green {
            background: linear-gradient(342deg, #1EA69D 0%, #36C6BF 100%);
          }
          &.red {
            background: linear-gradient(150deg, #FFAA27 0%, #FF1D56 100%);
          }
        }

      }

    }
    .item-icon-wrap{
      height: 20px;
      .item-icon{
        font-size: 10px;
        color: #7F7F7F;
        height: 16px;
        line-height: 16px;
        padding: 0 4px;
        background: #F5F5F5;
        border-radius: 2px;
        & + .item-icon{
          margin-left: 4px;
        }
      }
    }
    .item-data{
      height: 25px;
      .tips-split {
        display: inline-block;
        vertical-align: 1px;
        margin: 0 2px;
      }
    }
    .loc {
      max-width: 100px;
      text-align: right;
    }
  }
  .mark-btn {
    width: 100%;
    height: 32px;
    border: 1px solid #EEEEEE;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 32px;
    text-align: center;
    background-color: #fff;
  }
  .letter {
    letter-spacing: -1px
  }
}
</style>
